<?php
if ($show_highlight !== FALSE && $campaign['highlight_interval'] > 0) {
    ?>
    <div id="campaign_highloght" class="modal hide fade" >
        <div class="modal-body">
            <?php echo $campaign['highlight'] ?>
        </div>
        <div class="modal-footer">
            <button class="btn btn-danger countdown" data-dismiss="modal" aria-hidden="true" disabled="disabled" data-loading-text="Closing..."></button>
        </div>
    </div>
    <script>
        $(function() {
            $('#campaign_highloght').modal({
                backdrop: 'static',
                keyboard: false,
                show: true
            });
            

            // Count Down //
            $('#campaign_highloght .modal-footer').hide().delay(1000).slideDown();
            var interval = <?php echo $campaign['highlight_interval'] ?>;
                var countdown = setInterval(function() {
                    if (interval <= 0)
                    {
                        $('#campaign_highloght .modal-footer .countdown').attr('disabled', false);
                        $('#campaign_highloght .modal-footer .countdown').html('ปิดหน้าต่าง');
                        clearInterval(countdown);
                    }
                    else
                    {
                        $('.countdown').html('ปิดภายใน ' + interval + ' วินาที');
                        interval--;
                    }
                }, 1000);
            
            $('#campaign_highloght').on('hide', function() {
                $.post('<?php echo base_url()?>ajax/do_watch_highlight',{hash:'<?php echo $hash?>',token:'<?php echo $show_highlight?>'});
            });


        });
    </script>
    <?php
}
?>



<div class="row">
    <!-- Battle Content Pane-->
    <div class="span8">
        <div class="is-block bottom campaign-description">
            <?php if (!isset($not_found)): ?>
                <div class="well no-bottom">
                    <?php
                    if ($this->session->flashdata('register')) {
                        ?>
                        <div class="alert alert-success"><i class="icon icon-exclamation-sign"></i> คุณได้ทำการสมัครเข้าร่วมการแข่งขันนี้แล้ว คุณสามารถยกเลิกการสมัครแข่งขันได้ที่ปุ่ม "<b>ยกเลิกการสมัครแข่งขัน</b>" ด้านล่างผลงานได้ทุกเมื่อ</div>
                        <?php
                    }
                    ?>
                    <h4><?php echo $candidate['title'] ?></h4>
                    <hr/>
                    <?php
                    $register_content = unserialize($candidate['register_content']);
                    if (!$user_id) {
                        $this->load->view('emergency_login');
                    } else if ($campaign['candidate_vdo'] > 0) {
                        ?>
                        <div id="galleria">
                            <?php
                            foreach ($register_content as $content):
                                ?>
                                <a href="http://www.youtube.com/watch?v=<?php echo $content ?>"><span class="video">Youtube</span></a>
                                <?php
                            endforeach;
                            ?>
                        </div>
                        <style>
                            /* This rule is read by Galleria to define the gallery height: */
                            #galleria{height:500px}
                        </style>
                        <script>
                            $(function() {
                                Galleria.configure({
                                    fullscreenDoubleTap: true,
                                    autoplay: true,
                                    transition: 'fade',
                                    lightbox: true
                                });
                                Galleria.loadTheme('<?php echo base_url() ?>js/galleria/themes/classic/galleria.classic.min.js');
                                Galleria.run('#galleria');
                            });

                        </script>
                        <?php
                    }
                    elseif ($campaign['candidate_image'] > 0) {
                        ?>
                        <div id="galleria">
                            <?php
                            foreach ($register_content as $content):
                                ?>

                                <img src="<?php echo base_url() ?>uploads/register_image/<?php echo $candidate['id'] ?>/<?php echo $content ?>"/>
                                <?php
                            endforeach;
                            ?>
                        </div>
                        <style>
                            /* This rule is read by Galleria to define the gallery height: */
                            #galleria{height:500px}
                        </style>
                        <script>
                            $(function() {
                                Galleria.configure({
                                    fullscreenDoubleTap: true,
                                    autoplay: true,
                                    transition: 'fade',
                                    lightbox: true
                                });
                                Galleria.loadTheme('<?php echo base_url() ?>js/galleria/themes/classic/galleria.classic.min.js');
                                Galleria.run('#galleria');
                            });

                        </script>
                        <?php
                    }
                    else {

                        foreach ($register_content as $content):
                            ?>
                            <div class="well" style="background:white;">
                                <?php echo $content ?>
                            </div>
                            <?php
                        endforeach;
                    }
                    ?>

                    <hr/>


                    <div class="candidate-share pull-left">
                        <div class="fb-like" data-href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $candidate['candidate_number'] ?>" data-send="true" data-layout="box_count" data-width="450" data-show-faces="false"></div>
                    </div>



                    <div class="candidate-vote pull-left">
                        <div class="candidate-info">
                            <img src="https://graph.facebook.com/<?php echo $candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid" alt="<?php echo $fullname ?>"/>
                            <div class="candidate-fullname pull-left">
                                <?php echo '[#' . $candidate['candidate_number'] . '] - ' . $candidate['firstname'] . ' ' . $candidate['lastname'] ?><br/>
                                <span>เมื่อ: <?php echo date('d-m-Y เวลา H:i:s', $candidate['created_at']) ?></span>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="candidate-vote-button btn-group">
                            <a href="javascript:void(0)" class="btn btn-success vote-up-button vote-button" <?php echo $is_voted ? 'disabled="disabled"' : '' ?>><i class="icon-thumbs-up"></i> โหวตชอบ</a>
                            <a href="javascript:void(0)" class="btn btn-danger vote-down-button vote-button"  <?php echo $is_voted ? 'disabled="disabled"' : '' ?>><i class="icon-thumbs-down"></i> โหวตไม่ชอบ</a>
                        </div>
                        <?php
                        if (!$is_voted):
                            ?>
                            <script>
                                $(function() {
                                    $('.vote-up-button').click(function() {
                                        $('.vote-button').button('loading');
                                        $.post('<?php echo base_url() ?>ajax/do_vote_up', {
                                            'hash': '<?php echo $hash ?>',
                                            'candidate_id': '<?php echo $candidate['id'] ?>'
                                        }, function() {


                                            $('.vote-button').button('reset');

                                            setTimeout(function() {
                                                $('.vote-button').attr('disabled', 'disabled');
                                            }, 0);
                                        });
                                    });

                                    $('.vote-down-button').click(function() {
                                        $('.vote-button').button('loading');
                                        $.post('<?php echo base_url() ?>ajax/do_vote_down', {
                                            'hash': '<?php echo $hash ?>',
                                            'candidate_id': '<?php echo $candidate['id'] ?>'
                                        }, function() {
                                            $('.vote-button').button('reset');
                                            setTimeout(function() {
                                                $('.vote-button').attr('disabled', 'disabled');
                                            }, 0);

                                        });
                                    });
                                });
                            </script>
                            <?php
                        endif;
                        ?>
                    </div>

                    <div class="candidate-stat pull-right">
                        <b class="view"><?php echo number_format($candidate['sum_view']) ?></b>
                        <?php
                        $positive = $candidate['sum_positive_score'] == 0 ? 1 : $candidate['sum_positive_score'];
                        $negative = $candidate['sum_negative_score'] == 0 ? 1 : $candidate['sum_negative_score'];
                        $sum = $positive + $negative;
                        $positive_bar = floor($positive * 100 / $sum);
                        $negative_bar = floor($negative * 100 / $sum);
                        ?>
                        <div class="progress progress-striped active">
                            <div class="bar bar-success" style="width: <?php echo $positive_bar . '%' ?>"></div>
                            <div class="bar bar-danger" style="width: <?php echo $negative_bar . '%' ?>"></div>
                        </div>

                        <span>
                            <i class="icon-thumbs-up"></i> <?php echo number_format($candidate['sum_positive_score']) ?>
                        </span>
                        <span>
                            <i class="icon-thumbs-down"></i> <?php echo number_format($candidate['sum_negative_score']) ?>
                        </span>
                        <span>
                            <i class="icon-bolt"></i>  <?php echo number_format($candidate['sum_wilson_score'], 2) ?>
                        </span>

                    </div>
                    <div class="clearfix"></div>
                    <hr/>



                    <div class="candidate-description">
                        <?php echo nl2br(strip_tags($candidate['description'])) ?>
                    </div>


                </div>
            <?php else: ?>
                <div class="alert alert-danger no-bottom"><i class="icon icon-exclamation-sign"></i> ไม่พบผู้เข้าแข่งขันนี้ หรือ กิจกรรมนี้ยังไม่มีคนเข้าร่วมแข่งขัน</div>
            <?php endif; ?>
        </div>

        <?php
        if (!isset($not_found) && $user_id):
            ?>
            <div class="is-block bottom">
                <div class="well" style="background:white">
                    <div class="fb-comments" data-href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $candidate['candidate_number'] ?>" data-width="560" data-num-posts="10"></div>
                </div>
            </div>
            <?php
        endif;
        ?>

    </div>

    <div class="span4">
        <?php
        if ($noview_candidate_list):
            ?>
            <div class="is-block bottom">
                <h5>ผู้แข่งขันที่ท่านยังไม่ได้ชม</h5>
                <hr/>
                <?php
                foreach ($noview_candidate_list as $other_candidate) {
                    ?>
                    <div class="other_candidate_wrapper">
                        <a href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $other_candidate['candidate_number'] ?>">
                            <img src="https://graph.facebook.com/<?php echo $other_candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid img-rounded"/>
                            <div class="description pull-right">
                                <?php echo '[#' . $other_candidate['candidate_number'] . '] - ' . $other_candidate['firstname'] . ' ' . $other_candidate['lastname'] ?><br/>
                                <span><?php echo $other_candidate['title'] ?></span>
                            </div>
                        </a>
                        <div class="clearfix"></div>
                    </div>

                    <?php
                }
                ?>
            </div>
            <?php
        endif;
        ?>

        <?php
        if (!isset($not_found)) {
            if ($is_my_register || $is_admin) {
                ?>
                <div class="is-block bottom">
                    <a class="btn btn-danger btn-block" href="<?php echo base_url() ?>contest/<?php echo $hash ?>/register/cancel/<?php echo hashids_encrypt($candidate['id']) ?>" onclick="return confirm('คุณแน่ใจที่ต้องการยกเลิกการสมัครแข่งขันนี้?')">ยกเลิกการสมัครแข่งขัน</a>
                </div>
                <?php
            }
        }
        ?>


        <?php
        if ($other_candidate_list):
            ?>
            <div class="is-block bottom">
                <h5>สุ่มผู้เข้าแข่งขันท่านอื่น</h5>
                <hr/>
                <?php
                foreach ($other_candidate_list as $other_candidate) {
                    ?>
                    <div class="other_candidate_wrapper">
                        <a href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $other_candidate['candidate_number'] ?>">
                            <img src="https://graph.facebook.com/<?php echo $other_candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid img-rounded"/>
                            <div class="description pull-right">
                                <?php echo '[#' . $other_candidate['candidate_number'] . '] - ' . $other_candidate['firstname'] . ' ' . $other_candidate['lastname'] ?><br/>
                                <span><?php echo $other_candidate['title'] ?></span>
                            </div>
                        </a>
                        <div class="clearfix"></div>
                    </div>

                    <?php
                }
                ?>
            </div>
            <?php
        endif;
        ?>

    </div>

</div>
<script>
                                function do_comment_candidate($href, $comment_id)
                                {
                                    $.post('<?php echo base_url() ?>ajax/do_comment_count', {'candidate_id': '<?php echo $candidate['id'] ?>'});
                                }
</script>

<?php
if ($campaign['status'] != 'pending' && rand(1, 5) == 1):
    ?>
    <script>
        $(function() {
            setTimeout(function() {
                $.post('<?php echo base_url() ?>ajax/do_sum_candidate', {'candidate_id': '<?php echo $candidate['id'] ?>'});
            }, 2000);
        });
    </script>

    <?php
endif;
?>
   